# coding:utf-8

"""
    1.捡豆子比赛, 3个小朋友一组
    2.每个小朋友捡起一颗豆子, 耗时1-3秒
"""

import random
import threading
import time

# 30颗豆子, 每颗豆子编号1~30
beans = list(range(1, 31))

# 声明一个变量, 存储最终的名次
ranking = []


def pick_beans():
    while len(beans) > 0:
        # 获取当前线程
        t = threading.current_thread()
        choice = random.choice(beans)
        beans.remove(choice)
        time.sleep(random.randint(1, 3))
        print("{}捡起了一颗豆子, 豆子编号: {}".format(t.name, choice))
    ranking.append(t.name)
    print("{}完成了比赛".format(t.name))


# 欣欣线程类
class XinXinThread(threading.Thread):
    def run(self):
        pick_beans()


# 明明线程类
class MingMingThread(threading.Thread):
    def run(self):
        pick_beans()


# 小慕线程类
class XiaoMuThread(threading.Thread):
    def run(self):
        pick_beans()


# 实例化每一个小朋友的线程对象
xinxin_thread = XinXinThread(name="欣欣")
mingming_thread = MingMingThread(name="明明")
xiaomu_thread = XiaoMuThread(name="小慕")

xinxin_thread.start()
mingming_thread.start()
xiaomu_thread.start()

xinxin_thread.join()
mingming_thread.join()
xiaomu_thread.join()

print("本次比赛的结果排名: {}".format(ranking))
